package com.sdvdxl.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationAdapter;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import com.sdvdxl.oauth.GrantManager;

/**
 * 客户端浏览器，功能比较弱<br>
 * 缺陷：不能自动清除cookie，cache导致无法用
 * 程序本身切换用户，必须手动清除浏览器的cookie和cache
 * @author sdvdxl (sdvdxl@163.com)
 *
 */
public class MiniInnerBrowser {
	private GrantManager gm;
	private Shell shell;
	private Browser browser;
	private Button btn;
	
	/**
	 * @wbp.parser.entryPoint
	 */
	public MiniInnerBrowser(GrantManager gm) {
		this.gm = gm;
		//open();
	}
	/**
	 * 打开窗口
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * 创建一个带浏览器的
	 * 固定大小的窗口
	 */
	private void createContents() {
		shell = new Shell(SWT.DIALOG_TRIM);
		shell.setSize(500, 400);
		shell.setText("百度账号授权页面");
		shell.setLayout(new FillLayout());
		browser = new Browser(shell, SWT.NONE);
		browser.setBounds(shell.getClientArea());
		browser.setUrl(gm.getGrantURL());
		
		browser.addLocationListener(new LocationAdapter() {
			@Override
			public void changed(LocationEvent e) {
				String accessToken = getAccessTokenCode(browser.getUrl());
				
				//取得accessToken之后
				if (accessToken!=null) {
					hasSuccessed();
					gm.setAccessToken(accessToken);
				}
				
			}
		});
	}
	
	private String getAccessTokenCode(String url) {
		Pattern p = Pattern.compile("(?<=access_token=).+(?=&session_secret)");
		String accessTokenCode = null;
		Matcher m = p.matcher(url);
		
		if (m.find()) {
			accessTokenCode = m.group();
		}
		
		return accessTokenCode;
	}
	
	private void hasSuccessed() {
		
		browser.stop();
		browser.dispose();
		Browser.clearSessions();
		btn = new Button(shell, SWT.MOZILLA);
		btn.setText("授权成功，点此关闭…");
		shell.layout();
		btn.addListener(SWT.MouseDown, new Listener() {
			@Override
			public void handleEvent(Event e) {
				shell.close();
			}
		});
	}
	
	/**
	 * 清除百度cookie
	 * bug 暂时没有找到好的解决方案
	 * @return 清除成功 true 否则 false
	 */
	public static boolean clearCookies() {
		boolean flag = true;
		
		flag = Browser.setCookie("BAIDUID=tytytytytytutyt;path=/;domain=.baidu.com;" 
				+ "expires=Thu, 01-Jan-1970 00:00:01 GMT",
				"baidu.com");
		flag = Browser.setCookie("BDUSS=-1;path=/;domain=.baidu.com;" 
				+ "expires=Thu, 01-Jan-1970 00:00:01 GMT", "baidu.com");
		flag = Browser.setCookie("BDUT=-1;path=/;domain=.baidu.com;" 
				+ "expires=Thu, 01-Jan-1970 00:00:01 GMT", "passport.baidu.com");
		return flag;
	}
}
